<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8" >
            <a-form-model-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
              <a-input v-model="model.materialCode" disabled  placeholder="请输入物料编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" disabled  placeholder="请输入物料名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noneSpec">
              <a-input v-model="model.noneSpec" disabled  placeholder="请输入规格型号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="主计量单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="meUnit">
              <j-dict-select-tag type="list" disabled  v-model="model.meUnit" dictCode="ji_unit" placeholder="请选择主计量单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouseId">
              <j-dict-select-tag type="list" disabled  v-model="model.warehouseId" dictCode="erp_warehouse,name,id" placeholder="请选择仓库" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="年月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="month">
              <a-month-picker placeholder="请选择年月" v-model="model.month" :format="dateMonthFormat"  @change="handleChange2" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="库存明细" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="bussInventoryDetailsTable.loading"
          :columns="bussInventoryDetailsTable.columns"
          :dataSource="bussInventoryDetailsTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="false"
          :row-style="rowStyle"
          >
        </j-vxe-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import Vue from 'vue'
  import moment from 'dayjs'

  export default {
    name: 'BussInventoryForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        modalWidth: 1300,
        category: '',
        code: '',
        quantity: '',
        dateMonthFormat:  'YYYY-MM',
        model:{
          month: '',
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['bussInventoryDetails',  ],
        tableKeys:['bussInventoryDetails', ],
        activeKey: 'bussInventoryDetails',
        // 库存明细
        bussInventoryDetailsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '出入库日期',
              key: 'inOutDate',
              // type: JVXETypes.date,
              width:"150px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '进厂日期',
              key: 'enterDate',
              // type: JVXETypes.date,
              width:"110px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '限用日期',
              key: 'restrictedDate',
              // type: JVXETypes.date,
              width:"110px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '生产日期',
              key: 'productionDate',
              // type: JVXETypes.date,
              width:"110px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '摘要',
              key: 'digest',
              // type: JVXETypes.date,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '车间',
              key: 'workshop',
              // type: JVXETypes.date,
              width:"150px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '库位',
              key: 'warehouseLocation',
              // type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '批次号',
              key: 'batch',
              // type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物料批号',
              key: 'materialBatchNumber',
              // type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title:'入库数量',
              key: 'inventoryQuantity',
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title:'出库数量',
              key: 'outboundQuantity',
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '操作类别',
              key: 'operationCategory',
              type: JVXETypes.select,
              width:"120px",
              dictCode: 'kc_business_type',
              disabled: false
            },
            {
              title: '单据编号',
              key: 'code',
              width:"150px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },

            {
              title: '单位',
              key: 'meUnit',
              type: JVXETypes.select,
              width:"60px",
              dictCode: 'ji_unit',
              disabled: false
            },
            {
              title: '库存数量',
              key: 'quantity',
              // type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '质检状态',
              key: 'qualityInspectionStatus',
              type: JVXETypes.select,
              width:"100px",
              dictCode: 'zj_status',
              disabled: false
            },
            {
              title: '主表备注',
              key: 'remark',
              // type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '子表备注',
              key: 'subtableRemarks',
              // type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态',
              key: 'status',
              type: JVXETypes.select,
              width:"100px",
              dictCode: 'kc_status',
              disabled: false
            },
            {
              title: '供应商',
              key: 'supplierName',
              // type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/inventory/bussInventory/add",
          edit: "/inventory/bussInventory/edit",
          queryById: "/inventory/bussInventory/queryById",
          bussInventoryDetails: {
            list: '/inventory/bussInventoryLedger/queryBussInventoryLedgerDetailsByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.model.month = moment().format(this.dateMonthFormat);
    },
    methods: {
      historyRow(row){
        this.category = row.id;
        this.code = row.code;
        this.quantity = String(row.quantity);
        this.$refs.selectInOutDetailList.add();
        this.$refs.selectInOutDetailList.title = "查看库存明细";
        this.$refs.selectInOutDetailList.disableSubmit = false;
      },
      addBefore(){
        this.bussInventoryDetailsTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        this.model.month = moment().format(this.dateMonthFormat);
          let params = { warehouseId: this.model.warehouseId, materialId: this.model.materialId,yearmonth: this.model.month}
          this.requestSubTableData(this.url.bussInventoryDetails.list, params, this.bussInventoryDetailsTable)
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          bussInventoryDetailsList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      handleChange2(value) {
        console.log(value.format('YYYY-MM')); // { key: "", label: "" }
        Vue.ls.set("queryParam.month",value.format('YYYY-MM'));
        this.model.month = value.format('YYYY-MM');
        let params = { warehouseId: this.model.warehouseId, materialId: this.model.materialId,yearmonth: this.model.month}
        this.requestSubTableData(this.url.bussInventoryDetails.list, params, this.bussInventoryDetailsTable)
      },
      rowStyle({row, rowIndex}) {
        console.log('row', row)
        console.log('rowIndex', rowIndex)
        if (row.batch !== null && row.batch !== '') {
          return { 'background-color': '#90EE90','font-weight': 'bolder!important'}
        }

      },
    }
  }
</script>

<style scoped>
</style>